<?php
include_once('general_include.php');

	$coach_id = $_POST['coach_id'];
	$user_type_id=$_SESSION['user_type'];	
	$selected_by=$_SESSION['user_id'];
	$video_id=$_POST['video_id'];
	$selected_for=$_POST['selected_for'];
	
	$SelectCatSql="SELECT user_like_id FROM ".TABLEPREFIX."_user_like WHERE video_id=".$video_id." AND selected_by=".$selected_by;
	$RsCatSql = $UserManagerObjAjax->GetRecords("Row",$SelectCatSql);
	$NumCatSql = count($RsCatSql);
	
	if($NumCatSql==0)
	{
		$table_name = TABLEPREFIX."_user_like";
      $Sql = "INSERT INTO ".TABLEPREFIX."_user_like set user_id='$coach_id',video_id='$video_id',user_type_id='$user_type_id',selected_for='$selected_for',
		selected_by='$selected_by',date_added='".date('Y-m-d')."'";
		$fields_values = array(   
								'user_id'  			=> $coach_id,
								'video_id'			=> $video_id,
								'user_type_id'  	=> $user_type_id,
								'selected_for'		=> $selected_for,						
								'selected_by'  		=> $selected_by,
								'date_added'  		=> date('Y-m-d')
								);		
		
		$msgreport= $UserManagerObjAjax->Execute($Sql);		
	}
	else
	{			
		$table_name = TABLEPREFIX."_user_like";

		$fields_values = array( 
								'user_id'  			=> $coach_id,
								'video_id'			=> $video_id,
								'user_type_id'  	=> $user_type_id,
								'selected_for'		=> $selected_for,						
								'selected_by'  		=> $selected_by,
								'date_added'  		=> date('Y-m-d')
								);		
							
		$where="user_like_id='".$RsCatSql['user_like_id']."'";
		$msgreport= $UserManagerObjAjax->UpdateRecords($table_name,$fields_values,$where);		
	}

	// likes and dislikes start
	
	$LikeSql = "SELECT count(*) AS numlike FROM ".TABLEPREFIX."_user_like WHERE selected_for='L' AND video_id=".$video_id ;
	$LikeArr = $UserManagerObjAjax->GetRecords('Row',$LikeSql);
			
	$UnlikeSql = "SELECT count(*) AS numunlike FROM ".TABLEPREFIX."_user_like WHERE selected_for='D' AND video_id=".$video_id ;
	$UnlikeArr = $UserManagerObjAjax->GetRecords('Row',$UnlikeSql);
			
	$RatingLike=$LikeArr['numlike'];
	$RatingUnlike=$UnlikeArr['numunlike'];
	
	$TotalRating=$RatingLike+$RatingUnlike;
	
	$RatingAvgLike=($RatingLike*100)/$TotalRating;
	$RatingAvgUnlike=($RatingUnlike*100)/$TotalRating;
	
	$WidthGreen=(210*$RatingAvgLike)/100;
	$WidthRed=(210*$RatingAvgUnlike)/100;
	
	$rating_bar.="<img src='images/green.gif' alt='' width='{$WidthGreen}px' height='4px' border='0' />";
	$rating_bar.="<img src='images/red.gif' alt='' width='{$WidthRed}px' height='4px'  border='0' />";

	echo $rating_bar.'-'.$RatingLike.'-'.$RatingUnlike;
	// likes and dislikes end
?>